<%-- 
    Document   : Home
    Created on : Mar 4, 2014, 10:36:09 PM
    Author     : Theodoro
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="error.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="java.util.Calendar"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.*"%>
<c:if test="${sessionScope.username == NULL}">
    <c:redirect url="http://webtek.org/Bonix/index.php"/>
</c:if>
<c:set scope="request" var="title" value="BONIX | HOME"/>
<!DOCTYPE html>
<html>
    <jsp:include page="head.jsp"/>
    <body>
        <jsp:include page="banner.jsp"/>


        <c:set var="startFromRecNo" value="0"/>
        <c:if test="${param.start != null}">
            <c:set var="startFromRecNo" value="${param.start}"/>
        </c:if>
        <sql:setDataSource user="root" password="" driver="com.mysql.jdbc.Driver"
                           url="jdbc:mysql://localhost/bonix"/>

        <c:set var="dt" value="<%=new java.util.Date()%>"/>
        <fmt:formatDate scope="session" var="now" value="${dt}" pattern="yyyy-MM-dd" />
        <c:set var="selectedDate" value="${sessionScope.now}"/>

        <c:if test="${sessionScope.pickedDate != null}">
            <c:set var="selectedDate" value="${sessionScope.pickedDate}"></c:set>
        </c:if>

        <sql:query var="contracts" maxRows="8" startRow="${startFromRecNo}">
            SELECT servp,cust,date
            FROM contracts
            WHERE servp ='${sessionScope.username}' and date = '${selectedDate}'
        </sql:query>

        <sql:query var="current">
            SELECT servp,cust,DAYNAME(date) as "dayname",MONTHNAME(date) as "month",DAY(date) as "day", YEAR(date) as "year"
            FROM contracts
            WHERE date = '${selectedDate}'
            LIMIT 1
        </sql:query>

        <sql:query var="customers">
            SELECT distinct sender
            FROM messages
            WHERE receiver ='${sessionScope.username}'
        </sql:query>

        <sql:query var="profiles" startRow="${startFromRecNo}">
            SELECT *
            FROM profile
        </sql:query>

        <sql:query var="messages">
            SELECT * 
            FROM messages
            WHERE receiver = '${sessionScope.username}' and status = 'unread'
            LIMIT 5
        </sql:query>

        <article id="content">

            <section class="container graph">
                <div class="tab">
                    <span class="title">Activities for: <span class="dtToday">        
                            <c:forEach items="${current.rows}" var="cur">
                                <c:choose>
                                    <c:when test="${cur.length > 1}">
                                        ${cur.dayname},${cur.month} ${cur.day}, ${cur.year}
                                    </c:when>
                                    <c:otherwise>
                                        ${selectedDate}
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                        </span>
                    </span>
                </div>
                <div class ="tab">
                    <form action="saveSelectedDate.jsp" method="POST">
                        <input type="date" name="chosenDate">
                        <input type="submit" value="search" class="btn btn-primary">
                    </form>
                </div>


                <div class="inner" id="bonixTable">
                    <c:if test="${startFromRecNo > 0}">
                        <c:url var="prevURL" value="home.jsp">
                            <c:param name="start" value="${startFromRecNo - 8}"></c:param>
                        </c:url>
                        <a href="${prevURL}">Previous</a>
                    </c:if>

                    <c:if test="${contracts.limitedByMaxRows}">
                        <c:url var="nextURL" value="home.jsp">
                            <c:param name="start" value="${startFromRecNo + 8}"></c:param>
                        </c:url>
                        <a href="${nextURL}">Next</a>
                    </c:if>

                    <table class="table table-hover table-condensed">
                        <thead>  
                            <tr>  
                                <th>Customer Name</th>
                                <th>Job Description</th>
                                <th>Contact Number</th>  
                            </tr>  
                        </thead> 
                        <tbody>
                            <c:forEach var="contract" items="${contracts.rows}">
                                <c:if test="${selectedDate == contract.date}">
                                    <tr>
                                        <c:url var="composeMessageURL" value="viewConversation.jsp">
                                            <c:param name="sender" value="${contract.cust}"/>
                                        </c:url>
                                        <td class="text-left"><a href="${composeMessageURL}">${contract.cust}</a></td>
                                        <td class="text-left">${contract.description}</td>
                                        <c:forEach var="account" items="${profiles.rows}">
                                            <c:if test="${account.username == contract.cust}">
                                                <td class="text-left">${account.contact}</td>
                                            </c:if>       
                                        </c:forEach>
                                    </tr>
                                </c:if>
                            </c:forEach>
                        </tbody>
                    </table>
                </div>

            </section>

            <section class="container todo">
                <div class="tab">
                    <span class="title">Create Transaction</span>
                </div>
                <div class="inner">
                    <div id="addTask">
                        <form action="savedata.jsp?source=home" method="POST">

                            <label>CUSTOMER:</label>                        
                            <select name="cust" style="width:180px;height:30px">
                                <c:forEach var="customer" items="${customers.rows}">
                                    <option style="width:180px;height:30px">${customer.sender}</option>
                                </c:forEach>
                            </select>
                            <br>                        
                            <label>JOB DESCRIPTION:</label>
                            <input type="text" name="description" class="form-control1">
                            <br>
                            <label>DATE:</label>
                            <input type="date" name="scheduledDate">
                            <br>
                            <input type="submit" value="Create" class="btn btn-primary">
                        </form>
                    </div>

                </div>
            </section>

            <section class="container others">
                <div class="tab">
                    <span class="title">New Messages</span>
                </div>
                <div class="inner">
                    <c:forEach var="message" items="${messages.rows}">
                        <c:url var="messageItemURL" value="checkMessage.jsp">
                            <c:param name="entry_id" value="${message.entry_id}" />
                        </c:url>
                        <li><a href="${messageItemURL}"><span class="badge badge-info">NEW</span></a>---${message.sender}</li>
                        </c:forEach>
                </div>
            </section>    

        </article>

    </body>
</html>
